package cn.edu.hbvtc.qms.web.struts.action;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.edu.hbvtc.qms.domain.Clazz;
import cn.edu.hbvtc.qms.domain.Department;
import cn.edu.hbvtc.qms.domain.Major;
import cn.edu.hbvtc.qms.domain.Role;
import cn.edu.hbvtc.qms.domain.Student;
import cn.edu.hbvtc.qms.domain.User;
import cn.edu.hbvtc.qms.service.IManagementService;
import cn.edu.hbvtc.qms.web.struts.form.RegisterActionForm;

public class RegisterAction extends AbstractBaseDispatchAction {

	private IManagementService managementService = null;

	public IManagementService getManagementService() {
		return managementService;
	}

	public void setManagementService(IManagementService managementService) {
		this.managementService = managementService;
	}

	// 跳转到注册页面
	public ActionForward reg(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<Department> deptList = managementService.findDepartments();
		request.setAttribute("deptList", deptList);

		return mapping.findForward("register");
	}

	// 调用service，完成注册操作
	public ActionForward regSave(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		RegisterActionForm raf = (RegisterActionForm) form;

		// 表单校验
		if (raf.getStuId() == null || "".equals(raf.getStuId())) {
			request.setAttribute("stuIdRequired",
					"请输入长度为11位字符的学号！例如：20090120101");

			List<Department> deptList = managementService.findDepartments();
			request.setAttribute("deptList", deptList);

			return mapping.findForward("register");
		} else {
			if (raf.getStuId().length() != 11) {

				request.setAttribute("stuIdError",
						"请输入长度为11位字符的学号！例如：20090120101");

				List<Department> deptList = managementService.findDepartments();
				request.setAttribute("deptList", deptList);

				return mapping.findForward("register");
			} else {
				Student student = managementService.findStudentByStuId(raf
						.getStuId());
				if (student != null) {
					request.setAttribute("stuIdExist", "您的学号已注册，请重新输入！");

					List<Department> deptList = managementService
							.findDepartments();
					request.setAttribute("deptList", deptList);

					return mapping.findForward("register");

				}
			}

		}

		if (raf.getNickname() == null || "".equals(raf.getNickname())) {
			request.setAttribute("nicknameRequired", "请输入真实姓名！");

			List<Department> deptList = managementService.findDepartments();
			request.setAttribute("deptList", deptList);

			return mapping.findForward("register");
		}

		if (raf.getSex() != '男' && raf.getSex() != '女') {
			request.setAttribute("sexRequired", "请选择性别！");

			List<Department> deptList = managementService.findDepartments();
			request.setAttribute("deptList", deptList);

			return mapping.findForward("register");
		}

		if (raf.getIDCard() == null || "".equals(raf.getIDCard())) {
			request.setAttribute("IDCardRequired", "请输入身份证号码！");

			List<Department> deptList = managementService.findDepartments();
			request.setAttribute("deptList", deptList);

			return mapping.findForward("register");
		} else {
			Pattern p = Pattern
					.compile("^[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])((\\d{4})|\\d{3}[A-Z|a-z])$");
			Matcher m = p.matcher(raf.getIDCard());
			if (!m.matches()) {
				request.setAttribute("IDCardError", "身份证号码输入有误！");

				List<Department> deptList = managementService.findDepartments();
				request.setAttribute("deptList", deptList);

				return mapping.findForward("register");
			} else {
				User userOfStu = managementService.findUserByUsername(raf
						.getIDCard());
				if (userOfStu != null) {
					request.setAttribute("IDCardExist", "该身份证号码已被注册，请核对后重新输入！");

					List<Department> deptList = managementService
							.findDepartments();
					request.setAttribute("deptList", deptList);

					return mapping.findForward("register");
				}
			}
		}

		if (raf.getDeptId() == 0) {
			request.setAttribute("deptIdRequired", "请选择所属院系！");

			List<Department> deptList = managementService.findDepartments();
			request.setAttribute("deptList", deptList);

			return mapping.findForward("register");
		}

		if (raf.getMajorId() == 0) {
			request.setAttribute("majorIdRequired", "请选择所属专业！");

			List<Department> deptList = managementService.findDepartments();
			request.setAttribute("deptList", deptList);

			Department dept = managementService.findDepartmentById(raf
					.getDeptId());

			List<Major> majorList = managementService.findMajorsOfDept(dept);
			request.setAttribute("majorList", majorList);

			return mapping.findForward("register");
		}

		if (raf.getClazzId() == 0) {
			request.setAttribute("clazzIdRequired", "请选择所属班级！");

			List<Department> deptList = managementService.findDepartments();
			request.setAttribute("deptList", deptList);

			Department dept = managementService.findDepartmentById(raf
					.getDeptId());

			List<Major> majorList = managementService.findMajorsOfDept(dept);
			request.setAttribute("majorList", majorList);

			Major major = managementService.findMajorById(raf.getMajorId());

			List<Clazz> clazzList = managementService.findClazzsOfMajor(major);
			request.setAttribute("clazzList", clazzList);

			return mapping.findForward("register");
		}

		Student student = new Student();
		PropertyUtils.copyProperties(student, raf);
		student.setUsername(raf.getIDCard());// 学生的身份证号码作为用户名
		student.setPassword(raf.getStuId());// 学生的学号作为密码

		// 设置学生角色为"USER"和"STUDENT"
		Role roleOfUser = managementService.findRoleByName("USER");
		Role roleOfStu = managementService.findRoleByName("STUDENT");
		Set<Role> roles = new HashSet<Role>();
		roles.add(roleOfUser);
		roles.add(roleOfStu);
		student.setRoles(roles);

		Clazz clazz = managementService.findClazzById(raf.getClazzId());
		student.setClazz(clazz);
		managementService.saveStudent(student);

		request.setAttribute("student", student);

		return mapping.findForward("registerSave");
	}
}
